class Solution {
public:
    vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {
        vector<vector<int>> diff(n + 2, vector<int>(n + 2));
        for (auto& q : queries) {
            diff[q[0] + 1][q[1] + 1]++;
            diff[q[0] + 1][q[3] + 2]--;
            diff[q[2] + 2][q[1] + 1]--;
            diff[q[2] + 2][q[3] + 2]++;
        }

        vector<vector<int>> res(n, vector<int>(n));
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {
                diff[i + 1][j + 1] += diff[i][j + 1] + diff[i + 1][j] - diff[i][j];
                res[i][j] = diff[i + 1][j + 1];
            }
        return res;
    }
};